package com.app.controller;


import com.app.common.Result;
import com.app.model.*;
import com.app.service.*;
import com.app.utils.CommonHelp;
import com.app.utils.IpUtil;
import com.app.utils.TokenUtils;
import com.app.vo.ConsumptionOrderVo;
import com.app.vo.ConsumptionVoucherVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 消费多少送多少订单记录 前端控制器
 * </p>
 *
 * @author yoominic
 * @since 2024-07-21
 */
@RestController
@RequestMapping("/consumptionOrder")
public class ConsumptionOrderController {


    @Autowired
    private UserService userService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private BusinessService businessService;
    @Autowired
    private IConsumptionOrderService consumptionOrderService;



    @GetMapping("/loginTest")
    public Result loginTest(String userid){
        String token = TokenUtils.sign(String.valueOf(userid));
        return Result.success(token);
    }


    @GetMapping("/getClassificationConfig")
    public Result getClassificationConfig(){
        return consumptionOrderService.getClassificationConfig();
    }

    /***************************************消费多少送多少系统分成配置*********************************************************/


    /**
     * 消费多少送多少系统配置列表
     * @param page
     * @param limit
     * @param businessName
     * @param request
     * @return
     */
    @GetMapping("queryWebConsumptionDivideConfig")
    public Result queryWebConsumptionDivideConfig(@RequestParam(defaultValue = "0") Integer page,
                                                  @RequestParam(defaultValue = "10") Integer limit,String businessName,
                                                  HttpServletRequest request){
        return consumptionOrderService.queryWebConsumptionDivideConfig(page, limit, businessName);
    }


    /**
     * 新增/修改  消费多少系统配置
     * @param config
     * @param request
     * @return
     */
    @PostMapping("saveWebConsumptionDivideConfig")
    public Result saveWebConsumptionDivideConfig(@RequestBody ConsumptionDivideConfig config,HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.parseInt(adminId));
        if (adminUser == null) {
            return Result.error(405, "用户不存在");
        }
        return consumptionOrderService.saveWebConsumptionDivideConfig(config,adminUser);
    }


    @GetMapping("getWebConsumptionDivideConfigById")
    public Result getWebConsumptionDivideConfigById(Integer id){
        return consumptionOrderService.getWebConsumptionDivideConfigById(id);
    }



    /***************************************小程序消费多少送多少*********************************************************/
    /**
     * 消费多少送多少   预约
     * @return
     */
    @PostMapping("/reservationConsumptionOrder")
    public synchronized Result reservationConsumptionOrder(@RequestBody ConsumptionOrder consumptionOrder, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {return Result.error(405, "token不存在");}
        String openId = TokenUtils.verify(token);
        UserModel user = userService.getUserByOpenid(openId);
        if (user == null) {return Result.error(405, "用户不存在");}
        if (1!=user.getVip()){return Result.error(201, "仅会员参与!");}
//        //校验活动是否存在
//        ActivityModel activity = activityService.getById(consumptionOrder.getActivityId());
//        if(activity == null){return Result.error(201, "活动不存在");}
        //校验商家是否存在
        BusinessModel businessModel= businessService.getById(consumptionOrder.getBusinessId());
        if(businessModel == null){return Result.error(201, "商家不存在");}
        //校验库存
//        if (activity.getNumber() <= 0) {return Result.error(201, "活动库存不足");}
        return consumptionOrderService.reservationConsumptionOrder(consumptionOrder,user);
    }


    /**
     * 后台管理  查询  消费多少送多少得订单
     * @return
     */
    @GetMapping("queryWebConsumptionOrderList")
    public Result queryWebConsumptionOrderList(@RequestParam(defaultValue = "0") Integer page,
                                               @RequestParam(defaultValue = "10") Integer limit,
                                               @RequestParam(required = false) String beginTime,
                                               @RequestParam(required = false) String endTime,
                                               @RequestParam(required = false) String status,
                                               HttpServletRequest request){
        return consumptionOrderService.queryWebConsumptionOrderList(page,limit,beginTime,endTime,status);
    }


    /**
     * 后台管理  --------  获取订单下的券列表
     * @param orderId
     * @return
     */
    @GetMapping("queryWebConsumptionOrderVoucherListById")
    public Result queryWebConsumptionOrderVoucherListById( String orderId){
        return consumptionOrderService.queryWebConsumptionOrderVoucherListById(orderId);
    }


    /**
     * 查询我的 消费多少送多少  订单
     * @param page
     * @param limit
     * @param request
     * @return
     */
    @GetMapping("queryConsumptionOrderList")
    public Result queryConsumptionOrderList(@RequestParam(defaultValue = "0") Integer page,
                                            @RequestParam(defaultValue = "10") Integer limit,
                                            String status,
                                            HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {return Result.error(405, "token不存在");}
        String openId = TokenUtils.verify(token);
        UserModel user = userService.getUserByOpenid(openId);
        if (user == null) {return Result.error(405, "用户不存在");}
        ConsumptionOrderVo vo = consumptionOrderService.queryConsumptionOrderList(page,limit,user.getId(),Integer.valueOf(status));
        return Result.success(vo);
    }


    /**
     * 我的卡券
     * @param page
     * @param limit
     * @param status
     * @param request
     * @return
     */
    @GetMapping("queryMyConsumptionVoucher")
    public Result queryMyConsumptionVoucher(@RequestParam(defaultValue = "0") Integer page,
                                            @RequestParam(defaultValue = "10") Integer limit,
                                            String status,
                                            HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {return Result.error(405, "token不存在");}
        String openId = TokenUtils.verify(token);
        UserModel user = userService.getUserByOpenid(openId);
        if (user == null) {return Result.error(405, "用户不存在");}
        ConsumptionVoucherVo vo = consumptionOrderService.queryMyConsumptionVoucher(page,limit,user.getId(),status);
        return Result.success(vo);
    }


    /**
     * 核销  消费多送二维码  订单核销
     * @return
     */
    @PostMapping("/writeOffConsumptionOrder")
    public synchronized Result writeOffConsumptionOrder(@RequestBody ConsumptionOrder consumptionOrder, HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {return Result.error(405, "token不存在");}
        String openId = TokenUtils.verify(token);
        UserModel user = userService.getUserByOpenid(openId);
        if (user == null) {return Result.error(405, "用户不存在");}
        Result result = consumptionOrderService.writeOffConsumptionOrder(consumptionOrder,user.getPhone());
        return result;
    }


    @GetMapping("getBusinessInfoBycode")
    public Result getBusinessInfoBycode(String verificationCode,HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {return Result.error(405, "token不存在");}
        String openId = TokenUtils.verify(token);
        UserModel user = userService.getUserByOpenid(openId);
        if (user == null) {return Result.error(405, "用户不存在");}
        Result result = consumptionOrderService.getBusinessInfoBycode(verificationCode);
        return result;
    }


    /**
     * 评价订单
     * @param consumptionOrder
     * @param request
     * @return
     */
    @PostMapping("/commentConsumptionOrder")
    public Result commentConsumptionOrder(@RequestBody ConsumptionOrder consumptionOrder, HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {return Result.error(405, "token不存在");}
        String openId = TokenUtils.verify(token);
        UserModel user = userService.getUserByOpenid(openId);
        if (user == null) {return Result.error(405, "用户不存在");}
        Result result = consumptionOrderService.commentConsumptionOrder(consumptionOrder);
        return result;
    }


    @Autowired
    private AdminUserService adminUserService;
    /**
     * 核销  消费多送二维码  分成券核销
     * @return
     */
    @PostMapping("/writeOffConsumptionOrderVoucher")
    public synchronized Result writeOffConsumptionOrderVoucher(@RequestBody ConsumptionVoucher voucher, HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {return Result.error(405, "token不存在");}
        String openId = TokenUtils.verify(token);
        UserModel user = userService.getUserByOpenid(openId);
        if (user == null) {return Result.error(405, "用户不存在");}

        QueryWrapper<AdminUserModel> adminUserModelQueryWrapper=new QueryWrapper<>();
        adminUserModelQueryWrapper.eq("phone",user.getPhone());
        AdminUserModel regionalGenerationUser=this.adminUserService.getOne(adminUserModelQueryWrapper);
        if(regionalGenerationUser==null)
        {
            return Result.error(201, "非区代用户,禁止核销兑换券");
        }
        if(regionalGenerationUser.getRoleId()!=4)
        {
            return Result.error(201, "非区代用户,禁止核销兑换券");
        }


        Result result = consumptionOrderService.writeOffConsumptionOrderVoucher(voucher,user);
        return result;
    }


    /**
     * 分享  分享分成券  分享券接收
     * @return
     */
    @PostMapping("/handleShareConsumerCoupon")
    public  synchronized Result handleShareConsumerCoupon(@RequestBody ConsumptionVoucher voucher, HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {return Result.error(405, "token不存在");}
        String openId = TokenUtils.verify(token);
        UserModel user = userService.getUserByOpenid(openId);
        if (user == null) {return Result.error(405, "用户不存在");}
        Result result = consumptionOrderService.handleShareConsumerCoupon(voucher,user);
        return result;
    }





}

